package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.ApartmentInfo;
import com.hpf.lease.model.bean.GraphInfo;
import com.hpf.lease.web.admin.dto.ApartmentSearchSubmitDto;
import com.hpf.lease.web.admin.dto.ApartmentSubmitDto;
import com.hpf.lease.web.admin.mapper.*;
import com.hpf.lease.web.admin.service.ApartmentInfoService;
import com.hpf.lease.web.admin.service.FileService;
import com.hpf.lease.web.admin.service.GraphInfoService;
import com.hpf.lease.web.admin.vo.ApartmentInfoVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author GOD
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService {

    @Resource
    ApartmentFacilityMapper apartmentFacilityMapper;
    @Resource
    ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Resource
    ApartmentLabelMapper apartmentLabelMapper;
    @Resource
    GraphInfoMapper graphInfoMapper;
    @Resource
    GraphInfoService graphInfoService;
    @Resource
    ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    FileService fileService;
    @Override
    public boolean updateReleaseStatusById(Long id, String status) {
        return this.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .set(ApartmentInfo::getIsRelease,status)
                .eq(ApartmentInfo::getId,id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateInfo(ApartmentSubmitDto submitDto) {
        long id = submitDto.getId() == null ? -1L :submitDto.getId();
        List<GraphInfo> graphList = null;   //minio中存储的图片
        ApartmentInfo one = this.getOne(Wrappers.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getName, submitDto.getName())
                .eq(ApartmentInfo::getDistrictId, submitDto.getDistrictId()));
        if (one != null && !one.getId().equals(id))
            throw new CommonException(ResultCodeEnum.APARTMENT_NAME_ERROR);
        if (id != -1L){
            apartmentFacilityMapper.deleteInfo(id);
            apartmentLabelMapper.deleteInfo(id);
            apartmentFeeValueMapper.deleteInfo(id);
            graphList = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                    .eq(GraphInfo::getItemType, 1)
                    .eq(GraphInfo::getItemId, id));
            graphInfoMapper.deleteApartmentInfo(id);
        }
        ApartmentInfo apartmentInfo = new ApartmentInfo();
        BeanUtils.copyProperties(submitDto,apartmentInfo);
        Map<String,String>areaNames = apartmentInfoMapper.selectAreaNames(submitDto.getDistrictId());
        apartmentInfo.setDistrictName(areaNames.get("dName"));
        apartmentInfo.setCityName(areaNames.get("cName"));
        apartmentInfo.setProvinceName(areaNames.get("pName"));
        this.saveOrUpdate(apartmentInfo);

        long nowId = apartmentInfo.getId();
        List<Long> facilityInfoIds = submitDto.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds))
            apartmentFacilityMapper.insertInfo(nowId,facilityInfoIds);
        List<Long> labelIds = submitDto.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds))
            apartmentLabelMapper.insertInfo(nowId,labelIds);
        List<Long> feeValueIds = submitDto.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds))
            apartmentFeeValueMapper.insertInfo(nowId , feeValueIds);
        List<GraphInfo> graphInfoList = submitDto.getGraphInfoList();
        if(CollectionUtils.isEmpty(graphInfoList))
            graphInfoMapper.insertInfo(1 , nowId , graphInfoList);
        if (CollectionUtils.isEmpty(graphList))
            fileService.deleteImageBatch(graphList);
        return true;
    }

    @Override
    public Page<ApartmentInfoVo> getPageItem(ApartmentSearchSubmitDto submitDto) {
        Page<ApartmentInfoVo> page = new Page<>(submitDto.getCurrent(),submitDto.getSize());
        List<ApartmentInfoVo> pageItem = apartmentInfoMapper.getPageItem(submitDto);
        page.setRecords(pageItem);
        return page;
    }
}




